package com.liwenfeng.bigevent.controller;

import com.liwenfeng.bigevent.pojo.Article;
import com.liwenfeng.bigevent.pojo.PageBean;
import com.liwenfeng.bigevent.pojo.Result;
import com.liwenfeng.bigevent.service.ArticleService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@CrossOrigin(origins = "*")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("")
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize, @RequestParam(required = false) String categoryId, @RequestParam(required = false) String state) {
        PageBean<Article> pageBean = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
//        if (article.getState() != "已发布" && article.getState() != "草稿") return Result.error("文章状态填写错误");
        articleService.add(article);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<Article> getById(Integer id){
        Article article=articleService.getById(id);
        return Result.success(article);
    }

    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }
}
